package com.gjt.zyglxt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.gjt.zyglxt.dao.IUsersDao;
import com.gjt.zyglxt.model.Users;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


/**
 * UsersController
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Resource
    private IUsersDao usersDao;

   @ApiOperation(value = "登录",notes = "登录")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name",value = "用户名",required = true,paramType = "path"),
        @ApiImplicitParam(name = "pass",value = "密码",required = true,paramType = "path")
    })
    @GetMapping("/login")
    public Users Login(@PathVariable String name,@PathVariable String pass){
          return usersDao.Login(name, pass);
    } 
    
    @ApiOperation(value = "查询管理员",notes = "查询管理员")
    @PostMapping("/findAll")
    public Map<String,Object> findAll(){
      List<Users> list = usersDao.findAll();
      Map<String,Object> map = new HashMap<String,Object>();
      map.put("code", 0);
      map.put("msg", "查询成功");
      map.put("count", list.size());
      map.put("data", list);
      return map;
    }

    @ApiOperation(value = "添加/修改管理员",notes = "添加/修改管理员")
    @PostMapping("/addOredit")
    public Users addOrEdit(@RequestBody Users u){
        return usersDao.save(u);
    }

    @ApiOperation(value = "删除",notes = "删除")
    @DeleteMapping("/{id}")
    public void del(@PathVariable Integer id){
      usersDao.deleteById(id);
    }
  }